﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Web.UI;


namespace BL.BIZControls
{   
    public enum ActionType { Save, SaveClose, Cancel, Delete }
    public delegate void ButtonsHandler(object sender, ActionType actionType);
    public delegate void DoActionHandler(object sender, string selectedAction);
    public delegate void ControlLoaded(object sender, EventArgs e);
    public delegate void CycleError(object sender, EventArgs e);
    public delegate void ModalControlClosing(object sender, EventArgs e);
    public delegate void ModalCommand(object sender, EventArgs e);

    public enum BIZControl
    {
        DemoBusinessType,
        DemoCompany, DemoCompanyList,
        DemoEmployee, DemoEmployeeList,
        DEMOProject, DEMOProjectList, DEMOProjectTask, DEMOProjectTaskList,DEMOProjectMemberList,
        DEMOCustomerList, DEMOCustomer,

        User, UserList,
        Group,GroupList, GroupPermissionList,       
        Role, RolesList, RolePermissionList,
        /// <summary>
        /// Rättighetsgränssnittet till olika objekt tex vem som har vilken roll på en mapp i arkivet
        /// </summary>
        ObjectRoleList,

        Test,
        FileArchiveList, FolderList, Folder, FileUpload, File

    }


    
    public enum VisibleState
    {
        Closed,Underlying, Active 
    }
    
    public class ControlContainerBase : UserControl
    {
        public Control ContainedControl { get; set; }
        public Control ContainedControlContainer { get; set; }
        public ControlContainerButtonsGUI ButtonsControl { get; set; }
        public bool ContainedControlExists { get { return ContainedControlContainer.Controls.Count > 0 ? true : false; } }
        public string ControlPath { get { return ViewState["ControlPath"] != null ? (string)ViewState["ControlPath"] : null; } set { ViewState["ControlPath"] = value; } }
        public string Title { get { return ViewState["Title"] != null ? (string)ViewState["Title"] : ""; } set { ViewState["Title"] = value; } }
        public event ButtonsHandler ButtonsClick;
      
        public BIZListControl ContainedListControl {get {return ContainedControl is BIZListControl ? (BIZListControl)ContainedControl : null;}}
        public BIZEditControl ContainedEditControl { get { return ContainedControl is BIZEditControl ? (BIZEditControl)ContainedControl : null; } }

        /// <summary>
        /// När man klickar på någon av Commandknapparna till modaladialogen så kan man hindra kontrollen hindra att tex fönstret stängs genom att sätta CancelCommand = true;
        /// När eventet är klart sätts CancelCommand tillbaka till defaultvärdet false så att nästa command kan genomföras.
        /// Detta kan användas vid tex en ja/nej fråga vid klick på spara.
        /// 
        /// Gör om det här till en veryfy funktion som man kan skicka in till alla typer av container controller så att man kan skicka in en text tex Är du säker på att du vill spara
        /// Bör även kunna kolla om något har ändrats
        /// </summary>
        public bool CancelCommand { get { return ViewState["CancelCommand"] != null ? (bool)ViewState["CancelCommand"] : false; } set { ViewState["CancelCommand"] = value; } }


        public string ValGroupSave { get { return ValGroupPrefix + "_SAVE"; } }
        public string ValGroupClose { get { return ValGroupPrefix + "_CLOSE"; } }
        public string ValGroupDelete { get { return ValGroupPrefix + "_DELETE"; } }
        protected string ValGroupPrefix { get { return ContainedControl == null ? "" : ContainedControl.GetType().Name; } }

        public void ShowButtons(bool OK, bool Cancel, bool DO, bool Delete)
        {
            ButtonsControl.ShowButtons(OK, Cancel, DO, Delete);
        }

        protected void CallButtonEvent(object source, ActionType actionType)
        {
            if (ButtonsClick != null)
                ButtonsClick(this, actionType);
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            if (ContainedControl != null)
            {                
                ButtonsControl.SetValGroupPrefix(ValGroupPrefix);
            }

        }



        protected string GetControlPath(BIZControl typeOfControl)
        {
            string returnValue = String.Empty;
            switch (typeOfControl)
            {
                
                case BIZControl.DemoBusinessType:
                    returnValue = "~/_controls/Demo/Edit_BusinessType.ascx";
                    break;
                case BIZControl.DemoCompany:
                    returnValue = "~/_controls/Demo/Edit_Company.ascx";
                    break;
                case BIZControl.DemoCompanyList:
                    returnValue = "~/_controls/Demo/List_Companies.ascx";
                    break;
                case BIZControl.DemoEmployee:
                    returnValue = "~/_controls/Demo/Edit_Employee.ascx";
                    break;
                case BIZControl.DemoEmployeeList:
                    returnValue = "~/_controls/Demo/List_Employees.ascx";
                    break;

                case BIZControl.DEMOCustomer:
                    returnValue = "~/_controls/DemoProject/Edit_Customer.ascx";
                    break;

                case BIZControl.DEMOCustomerList:
                    returnValue = "~/_controls/DemoProject/List_Customers.ascx";
                    break;

                case BIZControl.DEMOProject:
                    returnValue = "~/_controls/DemoProject/Edit_Project.ascx";
                    break;

                case BIZControl.DEMOProjectList:
                    returnValue = "~/_controls/DemoProject/List_Projects.ascx";
                    break;
                case BIZControl.DEMOProjectTask:
                    returnValue = "~/_controls/DemoProject/Edit_ProjectTask.ascx";
                    break;
                case BIZControl.DEMOProjectTaskList:
                    returnValue = "~/_controls/DemoProject/List_ProjectTasks.ascx";
                    break;
                case BIZControl.DEMOProjectMemberList:
                    returnValue = "~/_controls/DemoProject/List_ProjectMembers.ascx";
                    break;
                //Slut Demo

                case BIZControl.User:
                    returnValue = "~/_controls/Users/Edit_User.ascx";
                    break;
                case BIZControl.UserList:
                    returnValue = "~/_controls/Users/List_Users.ascx";
                    break;
                case BIZControl.Group:
                    returnValue = "~/_controls/Users/Edit_Group.ascx";
                    break;
                case BIZControl.GroupList:
                    returnValue = "~/_controls/Users/List_Groups.ascx";
                    break;
                case BIZControl.GroupPermissionList:
                    returnValue = "~/_controls/Security/List_SysGroupPermissions.ascx";
                    break;
                case BIZControl.Role:
                    returnValue = "~/_controls/Security/Edit_Role.ascx";
                    break;
                case BIZControl.RolesList:
                    returnValue = "~/_controls/Security/List_Roles.ascx";
                    break;
                case BIZControl.RolePermissionList:
                    returnValue = "~/_controls/Security/List_RolePermissions.ascx";
                    break;
                case BIZControl.ObjectRoleList:
                    returnValue = "~/_controls/Security/List_ObjectRoles.ascx";
                    break;
                case BIZControl.FileArchiveList:
                    returnValue = "~/_controls/Files/List_FileArchive.ascx";
                    break;
                case BIZControl.FolderList:
                    returnValue = "~/_controls/Files/List_Folders.ascx";
                    break;
                case BIZControl.Folder:
                    returnValue = "~/_controls/Files/Edit_Folder.ascx";
                    break;
                case BIZControl.FileUpload:
                    returnValue = "~/_controls/Files/Edit_FileUpload.ascx";
                    break;
                case BIZControl.File:
                    returnValue = "~/_controls/Files/Edit_File.ascx";
                    break;
                case BIZControl.Test:
                    returnValue = "~/_controls/Files/Test.ascx";
                    break;
            }
            if (!String.IsNullOrEmpty(returnValue))
                return returnValue;
            else
                return null;

        }
    

    }
}
